﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using JT808.Protocol;
using JT808.Protocol.MessageBody;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Extensions.Hosting;
using JT808.Gateway.Client;
using JT808.Gateway.SimpleClient.Services;
using JT808.Gateway.SimpleClient.Jobs;
using JT808.Gateway.WebApiClientTool;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;

namespace JT808.Gateway.SimpleClient
{
    class Program
    {
        static async Task Main(string[] args)
        {
            IConfiguration configuration = null;
            var serverHostBuilder = new HostBuilder()
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
                    config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                    //config.Build();
                })
                .ConfigureLogging((context, logging) =>
                {
                    logging.AddConsole();
                    logging.SetMinimumLevel(LogLevel.Trace);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddSingleton<ILoggerFactory, LoggerFactory>();
                    services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));

                    TestConfigOptions a = hostContext.Configuration.GetSection("TestConfigOptions").Get<TestConfigOptions>();
                    services.AddSingleton<TestConfigOptions>(a);

                    services.AddJT808Configure()
                            .AddClient()
                            .Builder()
                            .AddWebApiClientTool(new Uri("http://" + a.ServerIP + ":" + a.ServerHttpProt + "/"), a.ServerHttpPwd)
                            ;
                    services.AddHostedService<UpService>();
                    services.AddHostedService<Up2019Service>();
                    //services.AddHostedService<CallHttpClientJob>();
                });
            await serverHostBuilder.RunConsoleAsync();
        }
    }
}
